package com.dd.cloud.common.config.servlet;

import com.dd.cloud.common.constants.HealthConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: openfeign统一拦截器
 * @Author: czg
 * @Date: 2023/5/20 14:28
 * @Version: 1.0.0
 */
@Configuration
@Slf4j
public class FeignRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        //feign服务间通信加上登录信息
        HttpServletRequest request =
                ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                        .getRequest();
        String loginTransfer = request.getHeader(HealthConstants.CommonConstants.LOGIN_INFO);
        if (!StringUtils.isBlank(loginTransfer)){
            template.header(HealthConstants.CommonConstants.LOGIN_INFO, loginTransfer);

        }
    }

}

